package com.etc.sfts.biz.ipml;

import java.util.List;
import java.util.Map;

import com.etc.sfts.biz.PublicBiz;
import com.etc.sfts.dao.PublicDao;

import com.etc.sfts.entity.Movie;
import com.etc.sfts.entity.Public;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PublicBizImpl  implements PublicBiz{
	@Autowired
    PublicDao dao;

	public PublicDao getDao() {
		return dao;
	}

	public void setDao(PublicDao dao) {
		this.dao = dao;
	}

	@Override
	public boolean insertPublic(Public p) {
		if(p!=null) {
			return  dao.addPublic(p);
		}
		return false;
	}
	@Override
	public List<Public> getPublicMessage(Integer size, Integer page) {
		// TODO Auto-generated method stub
		return dao.queryPublicMessage(size, page);
	}
	@Override
	public Long queryPublicCount() {
		// TODO Auto-generated method stub
		return dao.getPublicCount();
	}
	@Override
	public boolean deletePublicMassage(Integer id) {
		if(id<0||id==null) {
			return false;
		}
		return dao.deletePublicMassage(id);
	}

	@Override
	public HSSFWorkbook exportPublicExcel(Map<String, Object> map) {
		//获取需要生成excel的数据
		List<Public> list = dao.getPublicToExcel(map);
		//判断是否为空
		if (list == null) {
			return null;
		}

		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		//创建一个excel的sheet
		HSSFSheet sheet = hssfWorkbook.createSheet("firstshett");
		//表头属性名
		String[] propertyName = {"ID","影院名称","影院地区","发布时间","发布电影名","电影语言","电影价格","电影影厅号","影院地址"};
		//获取表所需要的数量
		int length = propertyName.length;
		//-------设置第一行---------
		//设置行下面为第一行
		HSSFRow row = sheet.createRow(0);
		//设置列下面为第一行的第一列
		HSSFCell hssfCell = row.createCell(0);


		//设置字体
		HSSFFont hssfFont = hssfWorkbook.createFont();
		hssfFont.setFontName("黑体");
		//字体大小
		hssfFont.setFontHeightInPoints((short) 12);
		//设置样式
		HSSFCellStyle hssfCellStyle = hssfWorkbook.createCellStyle();
		//使用上面的字体样式
		hssfCellStyle.setFont(hssfFont);
		hssfCellStyle.setLocked(true);
		//自动换行
		hssfCellStyle.setWrapText(false);
		CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, length - 1);
		//把合并单元格设置进去
		sheet.addMergedRegion(rangeAddress);
		//设置样式
		hssfCell.setCellStyle(hssfCellStyle);
		hssfCell.setCellValue("车辆列表信息");
		//----------设置第二行-----------
		HSSFRow row2 = sheet.createRow(1);
		//字段名的字体
		HSSFFont hssfFont1 = hssfWorkbook.createFont();
		hssfFont1.setFontName("宋体");
		//字体大小
		hssfFont1.setFontHeightInPoints((short) 12);
		//列表样式
		HSSFCellStyle hssfCellStyle1 = hssfWorkbook.createCellStyle();
		//设置字体字体样式
		hssfCellStyle1.setFont(hssfFont1);
		hssfCellStyle1.setLocked(true);
		hssfCellStyle1.setWrapText(false);

		//设置行高
		row2.setHeight((short) 750);
		//创建列
		HSSFCell cell = null;
		for (int i = 0; i < length; i++) {
			cell = row2.createCell(i);
			//设置字段名
			cell.setCellValue(propertyName[i]);
			//设置样式
			cell.setCellStyle(hssfCellStyle1);
			//----设置表里面的内容--------------
			//设置字体
			HSSFFont font=hssfWorkbook.createFont();
			font.setFontName("宋体");
			font.setFontHeightInPoints((short)10);
			//设置样式
			HSSFCellStyle hssfCellStyle2=hssfWorkbook.createCellStyle();
			hssfCellStyle2.setFont(font);
			hssfCellStyle2.setWrapText(true);
			HSSFRow hssfRow=null;
			HSSFCell hssfCell1=null;
			int valueStatrRow=2;
			//赋值
			for (Public publicmassage:list
			) {
				hssfRow=sheet.createRow(valueStatrRow);
				//id
				hssfCell1=hssfRow.createCell(0);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_id());
				//影院名称
				hssfCell1=hssfRow.createCell(1);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_cinema());
				//影院地区
				hssfCell1=hssfRow.createCell(2);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_address());
				//发布时间
				hssfCell1=hssfRow.createCell(3);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_time());
				//电影名
				hssfCell1=hssfRow.createCell(4);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_movie());
				//电影语言
				hssfCell1=hssfRow.createCell(5);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_language());
				//电影价格
				hssfCell1=hssfRow.createCell(6);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_price());
				//电影厅
				hssfCell1=hssfRow.createCell(7);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_room());
				//影院具体地址
				hssfCell1=hssfRow.createCell(8);
				hssfCell.setCellStyle(hssfCellStyle2);
				hssfCell1.setCellValue(publicmassage.getPublish_deaddress());

				valueStatrRow++;



			}

		}
		return hssfWorkbook;
	}
}
